package cn.sccl.mybatis.junt;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import cn.sccl.mybatis.pojo.User;

/**
 * @author 饶爽
 * @time 2019年9月15日 上午12:56:20
 * @version 1.0.0
 * MyBatis
*/
public class MyBatisFirstTest {
    
    @Test
    //根据用户id查询一个用户
    public void testMyBatis() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //执行Sql语句 
        User user = sqlSession.selectOne("test.findUserById", 10);
        
        System.out.println(user);
        sqlSession.close();
    }
    //根据用户名称模糊查询用户列表 
    @Test
    public void testfindUserByUsername() throws Exception {
        //加载核心配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //执行Sql语句 
        List<User> users = sqlSession.selectList("test.findUserByUsername", "五");
        for (User user2 : users) {
            System.out.println(user2);
        }
        sqlSession.close();
    }
    //添加用户 
    @Test
    public void testInsertUser() throws Exception {
        //加载核心配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //执行Sql语句 
        User user = new User();
        user.setUsername("王维");
        user.setBirthday(new Date());
        user.setAddress("南京");
        user.setSex("男");
        sqlSession.insert("test.insertUser", user);
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
    }
    //更新用户 
    @Test
    public void testUpdataUserById() throws Exception {
        //加载核心配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //执行Sql语句 
        User user = new User();
        user.setId(27);
        user.setUsername("王维1212");
        user.setBirthday(new Date());
        user.setAddress("南京");
        user.setSex("男");
        sqlSession.update("test.updateUserById", user);
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
    }
    //删除用户 
    @Test
    public void testDeleteUserById() throws Exception {
        //加载核心配置文件
        InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        
        //执行Sql语句 
        sqlSession.delete("test.deleteUserById", 27);
        //提交事务
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
    }
}
